﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Ajax_Exchange : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        HttpContext c = HttpContext.Current;
        string rtn = "fail";
        Model.CMember modelcm = BLL.CMember.UserLoginInfo();

        int proid = ReadRequest.RequestInt("proid");
        Model.Product pro = BLL.Product.GetModel(proid);

        string usertrendsCode = ReadRequest.RequestHtmlEnCode("zhifupass");

        if (modelcm != null && pro != null)
        {
            if (modelcm.zhifuPassword != null)
            {
                Model.UsersAccount uaccout = BLL.UsersAccount.GetModeluser_id(modelcm.Id);
                string zhifupass = BLL.Security.MD5(usertrendsCode);
                #region
                if (modelcm.zhifuPassword.Equals(zhifupass))
                {
                    if (uaccout != null && uaccout.integralTotal >= pro.Price)
                    {
                        Model.IntegralExchange Integral = new Model.IntegralExchange();
                        //如果是兑换红包：执行插入GoldExchange，IntegralExchange
                        if (pro.CId == 11)
                        {
                            Model.GoldExchange gold = new Model.GoldExchange();
                            gold.UserId = modelcm.Id;
                            gold.Type = 1;//兑换类型：1.获得  2. 消费
                            gold.GoldCount = pro.Money;//兑换 或者消费的金币数量
                            gold.Content = "积分兑换:" + pro.Title;//兑换或者消费描述
                            gold.Addtime = DateTime.Now;
                            gold.IsSuccess = true;
                            BLL.GoldExchange.Add(gold);
                            //更新用户账户金币
                            uaccout.goldTotal = uaccout.goldTotal + pro.Money;
                            //兑换红包成功
                            Integral.IsSuccess = true;
                        }
                        else
                        {
                            //兑换道具成功单位发货
                            Integral.IsSuccess = false;
                        }
                        //否则执行插入IntegralExchange
                        Integral.UserId = modelcm.Id;
                        Integral.Type = 2;//兑换类型：1.获得  2. 消费 
                        Integral.Content = "积分兑换:" + pro.Title;//兑换或者消费描述
                        Integral.UseIntegral = pro.Price;//使用积分数
                        Integral.OrderId = DateTime.Now.ToString("yyyyMMddhhmmss");//订单号
                        Integral.Addtime = DateTime.Now;
                        //更新用户账户的积分
                        uaccout.integralTotal = uaccout.integralTotal - pro.Price;

                        long Integralid = BLL.IntegralExchange.Add(Integral);
                        if (Integralid > 0 && BLL.UsersAccount.Update(uaccout) > 0)
                        {
                            pro.KuCun = pro.KuCun - 1;
                            pro.CanyuUser = pro.CanyuUser + 1;
                            BLL.Product.Update(pro);
                            rtn = "sucess";
                        }
                        else
                        {
                            rtn = "兑换失败！";
                        }
                    }
                    else
                    {
                        rtn = "您的积分余额不足！";
                    }
                }
                else
                {
                    rtn = "您的支付密码错误！";
                }
                #endregion
            }
            else
            {
                rtn = "请设置支付密码！";
            }
        }
        else
        {
            rtn = "请重新登陆！";
        }

        Response.Write(rtn);
        Response.End();
    }
}